package com.bee.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bee.auth.client.AuthHolder;
import com.bee.user.common.domain.OrgDO;
import com.bee.user.common.vo.OrgVO;
import com.bee.user.mapper.OrgMapper;
import com.bee.user.service.OrgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 角色表(Role)表服务实现类
 *
 * @author makejava
 * @since 2019-08-27 10:19:14
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public OrgDO queryById(Long id) {
        return orgMapper.selectById(id);
    }

    @Override
    public PageInfo<OrgVO> queryPage(OrgVO orgVO, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<OrgVO> list = orgMapper.queryAll(orgVO);
        return new PageInfo<>(list);
    }

    @Override
    public OrgDO insert(OrgDO domain) {
        domain.setEnable(Boolean.TRUE);
        domain.setCreateTime(new Date());
        domain.setCreator(AuthHolder.getUser());
        domain.setUpdateTime(domain.getCreateTime());
        domain.setUpdator(domain.getCreator());
        orgMapper.insert(domain);
        return domain;
    }

    @Override
    public void update(OrgDO domain) {
        domain.setUpdator(AuthHolder.getUser());
        domain.setUpdateTime(new Date());
        orgMapper.updateById(domain);
    }

    @Override
    public boolean deleteById(Long id) {
        OrgDO OrgDO = orgMapper.selectById(id);
        OrgDO.setEnable(Boolean.FALSE);
        return orgMapper.updateById(OrgDO) > 0;
    }

    @Override
    public List<OrgDO> queryAllEnable() {
        OrgDO OrgDO = new OrgDO();
        OrgDO.setEnable(Boolean.TRUE);
        return orgMapper.selectList(Wrappers.query(OrgDO));
    }
}